import json
import os


def data_convert_to_json(accent_scp_path, wav_scp_path, train_path='../accent_train.json',
                         dev_path='../accent_dev.json'):
    dev_ratio = 0.1
    res_train = dict()
    res_dev = dict()
    os.makedirs(os.path.dirname(train_path), exist_ok=True)
    os.makedirs(os.path.dirname(dev_path), exist_ok=True)
    with open(accent_scp_path, 'r', encoding='utf-8') as f:
        accent = f.readlines()
    with open(wav_scp_path, 'r', encoding='utf-8') as f:
        wav = f.readlines()
    assert len(accent) == len(wav)
    num = len(accent)
    for index, (wav_str, accent_str) in enumerate(zip(wav, accent)):
        wav_id1, wav_path = wav_str.strip().split()
        wav_id2, wav_accent = accent_str.strip().split()
        assert wav_id1 == wav_id2
        if index < num * dev_ratio:
            res_dev[wav_id1] = {
                'spk_id': wav_accent,
                'wav': wav_path
            }
        else:
            res_train[wav_id1] = {
                'spk_id': wav_accent,
                'wav': wav_path
            }
    with open(train_path, 'w', encoding='utf-8') as f:
        json.dump(res_train, f, ensure_ascii=False, indent=4)
    with open(dev_path, 'w', encoding='utf-8') as f:
        json.dump(res_dev, f, ensure_ascii=False, indent=4)


if __name__ == "__main__":
    accent_path = "/home/work_nfs7/xlgeng/code_runner_gxl/ai/project/fangyan_project/dialects/accent_label/wav_text.scp"
    wav_path = "/home/work_nfs7/xlgeng/code_runner_gxl/ai/project/fangyan_project/dialects/accent_label/wav_accent.scp"
    # data_convert_to_json(accent_path, wav_path)
    data_convert_to_json('accent.scp', "wav.scp")
